package com.bds.tjj.service;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.bds.tjj.model.ConfidenData;
import com.bds.tjj.model.MonthData;
import com.bds.tjj.model.PropIndex;
import com.bds.tjj.model.YearData;
import com.bds.tjj.utils.ExportModel;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Criteria;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bds.tjj.utils.ExcelUtil;

@Component
public class ExportService {

    private static Logger logger = Logger.getLogger(ExportService.class);

    @Resource
    private NutDao dao;


    public HSSFWorkbook exportExcel(String type, String prop, int startTime, int endTime) throws Exception {
        type = URLDecoder.decode(type, "utf-8");
        prop = URLDecoder.decode(prop, "utf-8");

        List<ExportModel> array = new ArrayList<ExportModel>();
        JSONArray head = new JSONArray();

        JSONArray tmp = generateList(type, prop, startTime, endTime);
        if (type.equals("y")) {
            head = generateHead("yy");
        } else {
            head = generateHead("ym");
        }

        ExportModel model = new ExportModel();
        model.setHead(head);
        model.setList(tmp);
        array.add(model);

        try {
            HSSFWorkbook wb = ExcelUtil.exportExcel(array);
            return wb;
        } catch (Exception e) {
            logger.info("", e);

        }
        return null;
    }

    private JSONArray generateList(String type, String prop, int startTime, int endTime) {
        List list = new ArrayList();
        JSONArray tmp = new JSONArray();

        if (type.equals("y")) {
            list = dao.query(YearData.class,
                    Cnd.where("prop", "=", prop).and("yy", ">=", startTime)
                            .and("yy", "<=", endTime)
                            .orderBy("yy", "desc"));
            PropIndex propIndex = new PropIndex();
            if (list.size() > 0) {
                propIndex = dao.fetch(PropIndex.class,
                        Cnd.where("prop", "=", prop));
            }
            for (YearData data : (List<YearData>) list) {
                data.setEn_name(propIndex.getEn_name());
                tmp.add(data);
            }
        } else {
            if (type.equals("m")) {
                list = dao.query(MonthData.class, Cnd.where("prop", "=", prop)
                        .and("ym", ">=", startTime).and("ym", "<=", endTime)
                        .orderBy("ym", "desc"));
                PropIndex propIndex = new PropIndex();
                if (list.size() > 0) {
                    propIndex = dao.fetch(PropIndex.class,
                            Cnd.where("prop", "=", prop));
                }
                for (MonthData data : (List<MonthData>) list) {
                    data.setEn_name(propIndex.getEn_name());
                    tmp.add(data);
                }
            } else {
                list = dao.query(ConfidenData.class, Cnd.where("prop", "=", prop)
                        .and("ym", ">=", startTime).and("ym", "<=", endTime)
                        .orderBy("ym", "desc"));
                PropIndex propIndex = new PropIndex();
                if (list.size() > 0) {
                    propIndex = dao.fetch(PropIndex.class,
                            Cnd.where("prop", "=", prop));
                }
                for (ConfidenData data : (List<ConfidenData>) list) {
                    data.setEn_name(propIndex.getEn_name());
                    tmp.add(data);
                }
            }
        }

        return tmp;
    }

    private JSONArray generateHead(String timeProp) {
        JSONArray head = new JSONArray();
        for (int i = 0; i < 4; i++) {
            JSONObject jsonObject = new JSONObject();
            switch (i) {
                case 0: {
                    jsonObject.put("title", "cn_name");
                    jsonObject.put("prop", "cn_name");
                    break;
                }
                case 1: {
                    jsonObject.put("title", "en_name");
                    jsonObject.put("prop", "en_name");
                    break;
                }
                case 2: {
                    jsonObject.put("title", "data");
                    jsonObject.put("prop", "data");
                    break;
                }
                case 3: {
                    jsonObject.put("title", "time");
                    jsonObject.put("prop", timeProp);
                    break;
                }
            }
            head.add(jsonObject);
        }
        return head;
    }

    public String getFileName(String prop) {
        PropIndex propIndex = dao.fetch(PropIndex.class,
                Cnd.where("prop", "=", prop));
        return propIndex.getView_name();
    }


}
